package com.xyzwps.harbor.service;

import com.xyzwps.harbor.model.VcodeScope;
import com.xyzwps.harbor.repo.VcodeScopeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;

import java.lang.RuntimeException;

@Service
public class DemoService {

    @Autowired
    private VcodeScopeRepository vsRepo;

    @Transactional
    public Mono<Void> demoTx(VcodeScope vs1, VcodeScope vs2, Boolean willErrorOccur) {
        return vsRepo.insert(vs1)
                .then(willErrorOccur ? Mono.error(new RuntimeException("error occurred" )) : Mono.empty())
                .then(vsRepo.insert(vs2));
    }
}